<template>
  <div>
    <a @click.prevent="close" class="back-class" href="#">返回</a>
    <div style="width: 60%; margin: 0 auto;">
      <el-row style="width: 100%">
        <el-col :span="24">
          <div>
            <el-card style="margin-bottom: 10px">
              <vxe-form :data="formValue" :rules="formRules" ref="vxeForm"
                        span="24"
                        title-width="100" title-align="right">
                <vxe-form-item title="需求名称" field="Name">
                  <vxe-input v-model="formValue.Name" clearable></vxe-input>
                </vxe-form-item>
                <vxe-form-item title="来源" field="Source">
                  <DictionarySelect v-model="formValue.Source" size="small" dictTypeCode="NEEDS_SOURCE" clearable></DictionarySelect>
                </vxe-form-item>
                <vxe-form-item title="优先级" field="Level">
                  <DictionarySelect v-model="formValue.Level" size="small" dictTypeCode="LEVEL" clearable></DictionarySelect>
                </vxe-form-item>
                <vxe-form-item title="阶段" field="Stage">
                  <DictionarySelect v-model="formValue.Stage" size="small" dictTypeCode="NEEDS_STAGE"></DictionarySelect>
                </vxe-form-item>
                <vxe-form-item title="状态" field="Status">
                  <DictionarySelect v-model="formValue.Status" size="small" dictTypeCode="NEEDS_STATUS"></DictionarySelect>
                </vxe-form-item>
                <vxe-form-item title="指派给" field="Acceptid">
                  <SelectProjectTeam v-model="formValue.Acceptid" :url="userApi" getPage="getPage" responseData="Users" :queryValue="{}"
                                     searchKeyword="keywords" column="Realname" clickValue="Userid"
                                     showValue="Realname" :showValueShow="[]">
                  </SelectProjectTeam>
                </vxe-form-item>
                <vxe-form-item title="预计工时" field="Tasktime">
                  <vxe-input v-model="formValue.Tasktime" type="number" min="0" max="127" clearable></vxe-input>
                </vxe-form-item>
                <vxe-form-item title="描述" field="Desc">
                  <wangeditor v-model="formValue.Desc"></wangeditor>
                </vxe-form-item>
                <vxe-form-item title="验收标准" field="Acceptance">
                  <wangeditor v-model="formValue.Acceptance"></wangeditor>
                </vxe-form-item>
                <vxe-form-item title="附件" field="Attachment">
                  <btnUpload v-model="formValue.Attachment" type="file" uploadApi="/v1/upload/"></btnUpload>
                </vxe-form-item>
                <vxe-form-item class="isLine">
                  <el-col>
                    <vxe-button status="primary" size="small" v-throttle="[save, `click`, 300]">保存</vxe-button>
                    <vxe-button @click="close" size="small">关闭</vxe-button>
                  </el-col>
                </vxe-form-item>
              </vxe-form>
            </el-card>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import searchComponent from "@/components/element/layoutComponents/searchFrom/searchComponent.vue";
import DictionarySelect from "@/components/element/select/DictionarySelect";
import SelectProjectTeam from "@/components/element/select/SelectProjectTeam";
import btnUpload from "@/components/element/btnUpload";
import projectNeedApi from "@/api/organizationApi/projectNeedApi";
import wangeditor from '@/components/element/wangeditor/index';
import userApi from '@/api/userApi'

export default {
  name: "needEdit",
  components: {
    searchComponent,
    SelectProjectTeam,
    DictionarySelect,
    btnUpload,
    wangeditor
  },
  data () {
    return {
      isShowStatus: false,
      userApi: userApi,
      formValue: {
        p: 1,
        pageoffset: 15
      },
      formRules: {
        Name: [
          { required: true, message: '请输入需求名称' }
        ],
        Source: [
          { required: true, message: '请选择来源' }
        ],
        Level: [
          { required: true, message: '请选择优先级' }
        ],
        Stage: [
          { required: true, message: '请选择阶段' }
        ]
      },
    }
  },
  mounted() {
    this.getNeedById()
    this.isShowStatus = false
    if (this.$route.query.id.length > 0) {
      this.isShowStatus = true
    }
  },
  methods: {
    getNeedById () {
      if (this.$route.query.id) {
        let a = {
          id: this.$route.query.id
        }
        projectNeedApi.getByid(a).then(res => {
          if (res) {
            this.formValue = res
            if (this.formValue.hasOwnProperty('Changed')) {
              this.formValue.Changed = this.formValue.Changed.toString()
            }
            if (this.formValue.hasOwnProperty('Level')) {
              this.formValue.Level = this.formValue.Level.toString()
            }
            if (this.formValue.hasOwnProperty('Source')) {
              this.formValue.Source = this.formValue.Source.toString()
            }
            if (this.formValue.hasOwnProperty('Stage')) {
              this.formValue.Stage = this.formValue.Stage.toString()
            }
            if (this.formValue.hasOwnProperty('Status')) {
              this.formValue.Status = this.formValue.Status.toString()
            }
            if (this.formValue.hasOwnProperty('Tasktime')) {
              this.formValue.Tasktime = this.formValue.Tasktime.toString()
            }
          }
        })
      }
    },
    save () {
      this.$refs.vxeForm.validate().then(() => {
        if (this.$route.query.id) {
          this.updaten()
        } else {
          this.saven()
        }
      }).catch(_ => {
        console.log(_)
      })
    },
    updaten () {
      let a = JSON.parse(JSON.stringify(this.formValue))
      a = Object.assign(a, {
        Projectid: this.$route.query.preId
      })
      projectNeedApi.update(a).then(res => {
        if (res && res.code) {
          this.close()
        }
        if (res.code) {
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 保存
    saven () {
      let a = JSON.parse(JSON.stringify(this.formValue))
      a = Object.assign(a, {
        Projectid: this.$route.query.preId
      })
      projectNeedApi.create(a).then(res => {
        if (res && res.code) {
          this.close()
        }
        if (res.code) {
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    close () {
      this.$router.push(`/view/project/needlist/${this.$route.query.preId}`)
    }
  }
}
</script>

<style scoped>
.isLine {
  width: 100%;
  text-align: center;
}
.back-class {
  color: #00d1b2;
  font-size: 20px;
}
</style>
